package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.commons.web.JsonResult;
import cn.tedu.csmall.product.pojo.param.SkuAddNewParam;
import cn.tedu.csmall.product.pojo.param.SpuAddNewParam;
import cn.tedu.csmall.product.pojo.vo.SkuListItemVO;
import cn.tedu.csmall.product.pojo.vo.SpuListItemVO;
import cn.tedu.csmall.product.pojo.vo.SpuStandardVO;
import cn.tedu.csmall.product.service.SkuService;
import cn.tedu.csmall.product.service.SpuService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/sku")
@Api(tags = "01. SKU管理模块")
@Validated
@Slf4j
public class SkuController {

    @Autowired
    private SkuService skuService;

    // http://localhost:9180/sku/add-new
    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('/pms/product/add-new')")
    @ApiOperation("新增SKU")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(SkuAddNewParam skuAddNewParam){
        log.debug("开始处理【新增SKU】的请求，参数:{}",skuAddNewParam);
        skuService.addNew(skuAddNewParam);
        return JsonResult.ok();
    }

    // http://localhost:9180/sku/list-by-spu
    @GetMapping("/list-by-spu")
    @PreAuthorize("hasAuthority('/pms/product/read')")
    @ApiOperation("根据SPU查询SKU列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "spuId", value = "SPU ID", paramType = "query", dataType = "long"),
    })
    public JsonResult listBySpuId(@Range(min = 1, message = "请提交有效的spuId值！") Long spuId){
        log.debug("开始处理【根据SPU查询SKU列表】的请求，参数：{}", spuId);
        PageData<SkuListItemVO> pageData = skuService.listBySpuId(spuId);
        return JsonResult.ok(pageData);
    }


    // http://localhost:9180/sku/9527
    @GetMapping("/{id:[0-9]+}")
    @PreAuthorize("hasAuthority('/pms/product/read')")
    @ApiOperation("根据ID查询SKU基本信息")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "SKU的ID",  paramType = "query", dataType = "long"),
    })
    public JsonResult getStandardById(@Range(min = 1, message = "请提交有效id！") @PathVariable Long id){
        log.debug("开始处理【根据id查询Sku的详细信息】的请求，参数：{}", id);
//        SpuStandardVO spuStandardVO = skuService.getStandardById(id);
//        return JsonResult.ok(spuStandardVO);
        return JsonResult.ok();
    }
}
